package com.zadtek.sharkRider.activity;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ImageView;
import android.widget.TextView;

import com.zadtek.sharkRider.R;
import com.zadtek.sharkRider.activity.adapter.MyMainAsyncTask;
import com.zadtek.sharkRider.activity.common.Common;
import com.zadtek.sharkRider.activity.common.Const;
import com.zadtek.sharkRider.activity.data.UserData;

/**
 * ez外卖骑士账号余额界面
 * 
 * @author Administrator
 * 
 */
@EActivity(R.layout.user_money)
public class UserMoneyActivity extends BaseActivity {


	@ViewById
	TextView usermoney;// 账户余额
	
	@ViewById
	TextView userdetail;//查看余额明细
	
	@ViewById
	TextView usersubmit;//申请提现
	
	@ViewById
	ImageView backid;

	@ViewById
	TextView topview; // 标题

	private SharedPreferences sp;

	private Handler mHandler;

	// 初始化界面信息
	@AfterViews
	void initViews() {
		topview.setText("账户余额");

		sp = getSharedPreferences(Const.PREF_NAME, Context.MODE_PRIVATE);
		String uid = sp.getString("uid", "");
		String account = sp.getString("account", "");
		usermoney.setText(account);
	}

	@Click(R.id.backid)
	void backonclik() {
		finish();
	}
	
	@Click(R.id.userdetail)
	void seekdetail(){
		Intent intent = new Intent(UserMoneyActivity.this,UserDetail.class);
		startActivity(intent);
	}
	
	@Click(R.id.usersubmit)
	void moneycask(){
		Intent intent = new Intent(UserMoneyActivity.this,MoneyTowithdrawal.class);
		startActivity(intent);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		mHandler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				// TODO Auto-generated method stub
				super.handleMessage(msg);
				switch (msg.what) {
				case Const.STATE_START_ERROR:
					Common.DisplayToast(UserMoneyActivity.this, "通信失败,请检查网络!",
							2);
					break;
				case Const.STATE_GETNUM_SUCC: // 获取骑士信息成功
					UserData qsdata = (UserData) msg.obj;
					String qsstatus = qsdata.getStatus();
					if (qsstatus != null && qsstatus.equals("1")) {
						writeSp(qsdata); // 先存入缓存
						usermoney.setText(qsdata.getAccount());
					}
					break;
				}
			}
		};

		// 获取用户信息
//		if (Common.isNetworkConnected(UserMoneyActivity.this)) {
//			new MyMainAsyncTask(UserMoneyActivity.this, mHandler)
//					.execute("getqsmsg");
//		}
	}

	// 将用户信息写入缓存中
	private void writeSp(UserData userdata) {
		sp = getSharedPreferences(Const.PREF_NAME, Context.MODE_PRIVATE);
		Editor editor = sp.edit();
		editor.putString("uid", userdata.getUid());
		editor.putString("uname", userdata.getRealname());
		editor.putString("phone", userdata.getPhone());
		editor.putString("account", userdata.getAccount());
		editor.putString("close", userdata.getClose());
		editor.putString("qsstatus", userdata.getQsstatus());
		editor.putString("back", userdata.getBank());
		editor.commit();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		// 获取用户信息
				if (Common.isNetworkConnected(UserMoneyActivity.this)) {
					new MyMainAsyncTask(UserMoneyActivity.this, mHandler)
							.execute("getqsmsg");
				}
	}
}
